<template>
  <div class="w-table">
    <el-table
      :data="listData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
      v-bind="childrenProps"
    >
      <el-table-column
        v-if="showSelectColumn"
        type="selection"
        align="center"
        width="60"
      >
      </el-table-column>
      <el-table-column
        v-if="showIndexColumn"
        type="index"
        align="center"
        width="80"
        label="序号"
      >
      </el-table-column>
      <template v-for="propItem in propList" :key="propItem.prop">
        <el-table-column v-bind="propItem" align="center" show-overflow-tooltip>
          <!-- 这里#default是拿到element里面定义好的值 -->
          <template #default="scope">
            <!-- 这里的：row="scope.row是要把值绑定到我这个插槽里面" -->
            <slot :name="propItem.prop" :row="scope.row" test="测试">
              {{ scope.row[propItem.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer" v-if="showFooter">
      <slot name="footer">
        <el-pagination
          :currentPage="page.currentPage"
          :page-size="page.pageSize"
          :page-sizes="[5, 10, 20]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="dataCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from "vue";

export default defineComponent({
  props: {
    page: {
      type: Object,
      default: () => ({ currentPage: 1, pageSize: 10 }),
    },
    listData: {
      type: Array as PropType<any>,
      required: true,
    },
    dataCount: {
      type: Number,
      default: 0,
    },
    propList: {
      type: Array as PropType<any>,
      required: true,
    },
    showIndexColumn: {
      type: Boolean,
      default: false,
    },
    showSelectColumn: {
      type: Boolean,
      default: false,
    },
    childrenProps: {
      type: Object,
      default: () => ({}),
    },
    showFooter: {
      type: Boolean,
      default: true,
    },
  },
  components: {},
  emits: ["selectionChange", "update:page"],
  setup(props, { emit }) {
    const handleSelectionChange = (value: any) => {
      emit("selectionChange", value);
    };
    const handleCurrentChange = (currentPage: any) => {
      emit("update:page", { ...props.page, currentPage });
    };
    const handleSizeChange = (pageSize: any) => {
      emit("update:page", { ...props.page, pageSize });
    };
    return {
      handleSelectionChange,
      handleSizeChange,
      handleCurrentChange,
    };
  },
});
</script>

<style scoped lang="less">
.footer {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
